package inter;

import symbols.*;
import lexer.*;

public class SetField extends Stmt {
    private Field fld;
    private Expr expr;
    private Access acc;
    private boolean accp = false;
    public SetField(Field f, Expr x) {
	this.fld = f;
	this.expr = x;
	if (f.fld instanceof Access) {
	    accp = true;
	    acc = (Access)f.fld;
	}
	if (check(f.fld.type, x.type) == null)
	    error("type error");
    }
    public Type check(Type p1, Type p2) {
	if (p1 instanceof Array || p2 instanceof Array)
	    return null;
	else if (Type.numeric(p1) && Type.numeric(p2))
	    return p2;
	else if (p1 == p2) return p2;
	else return null;

    }
    public void gen(int b, int a) {
	String s1 = expr.reduce().toString();
	if (accp) { // array
	    String s2 = acc.index.reduce().toString();
	    emit(fld.rec+" . "+
		 acc.array.toString()+" [ "+s2+" ] = "+s1);
	}
	else {
	    emit(fld.rec+" . "+fld.fld.toString()+" = "+s1);
	}
    }
}
